package cn.tedu.anjiadb.web;

import cn.tedu.anjiadb.ex.ServiceException;
import lombok.Data;

import java.io.Serializable;

@Data
public class JsonResult<T> implements Serializable {
    private Integer state;

    private String message;

    private T data;

    public static JsonResult<Void> ok(){
        return ok(null);
    }

    public static<T> JsonResult<T> ok(T data){
        JsonResult<T> jsonResult = new JsonResult();
        jsonResult.setState(ServiceCode.OK.getValue());
        jsonResult.setData(data);
        return jsonResult;
    }

    public static JsonResult<Void> fail(ServiceException e){
        return fail(e.getServiceCode(), e.getMessage());
    }

    public static JsonResult<Void> fail(ServiceCode serviceCode,String message){
        JsonResult<Void> jsonResult = new JsonResult();
        jsonResult.setState(serviceCode.getValue());
        jsonResult.setMessage(message);
        return jsonResult;
    }
}
